package com.dlyk.service.impl;

import com.dlyk.constant.Constants;
import com.dlyk.mapper.CustomerRemarkMapper;
import com.dlyk.model.CustomerRemark;
import com.dlyk.model.User;
import com.dlyk.query.CustomerRemarkQuery;
import com.dlyk.service.CustomerRemarkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerRemarkServiceImpl implements CustomerRemarkService {

    @Resource
    private CustomerRemarkMapper customerRemarkMapper;

    /**
     * 添加客户跟踪记录（客户备注）
     */
    @Override
    public int addCustomerRemark(CustomerRemarkQuery customerRemarkQuery) {

        CustomerRemark customerRemark = new CustomerRemark();

        // 使用BeanUtils工具将CustomerRemarkQuery对象的属性复制到CustomerRemark对象中
        BeanUtils.copyProperties(customerRemarkQuery, customerRemark);

        // 获取当前登录用户（创建者）
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        // 设置创建者
        customerRemark.setCreateBy(loginUser.getId());
        // 设置创建时间
        customerRemark.setCreateTime(new Date());
        // 设置删除状态（0正常，1删除）
        customerRemark.setDeleted(0);

        return customerRemarkMapper.insertSelective(customerRemark);
    }

    /**
     * 根据客户id查询客户的跟踪记录
     */
    @Override
    public PageInfo<CustomerRemark> getCustomerRemarkListByPage(Integer customerId, Integer current) {

        // 1、设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        // 2、查询
        List<CustomerRemark> customerRemarkList = customerRemarkMapper.selectCustomerRemarkListByActivityId(customerId);

        // 3、封装分页数据到PageInfo
        PageInfo<CustomerRemark> customerRemarkPageInfo = new PageInfo<>(customerRemarkList);

        return customerRemarkPageInfo;
    }

    /**
     * 编辑跟踪记录
     */
    @Override
    public int editCustomerRemark(CustomerRemarkQuery customerRemarkQuery) {

        CustomerRemark customerRemark = new CustomerRemark();

        // 使用BeanUtils工具将CustomerRemarkQuery对象的属性复制到CustomerRemark对象中
        BeanUtils.copyProperties(customerRemarkQuery, customerRemark);

        // 获取当前登录用户（创建者）
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        // 设置编辑者
        customerRemark.setEditBy(loginUser.getId());
        // 设置编辑时间
        customerRemark.setEditTime(new Date());

        return customerRemarkMapper.updateByPrimaryKeySelective(customerRemark);
    }

    /**
     * 根据id删除跟踪记录
     */
    @Override
    public int removeCustomerRemarkById(Integer id) {

        // 跟踪记录（备注）的删除是逻辑删除

        CustomerRemark customerRemark = new CustomerRemark();
        // 要进行逻辑删除的跟踪记录id
        customerRemark.setId(id);
        // 将这条跟踪记录标识为已删除
        customerRemark.setDeleted(1);

        // 更新记录
        return customerRemarkMapper.updateByPrimaryKeySelective(customerRemark);
    }

}

